package com.liantong.demo_part2.Controller;

import com.liantong.demo_part2.Service.UpdateService;
import com.liantong.demo_part2.Utils.JsonResult;
import com.liantong.demo_part2.Utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(tags = "规划方案的接口部分")
@Controller
@RequestMapping("/Access/update")
public class UpdateController {

    @Autowired
    private UpdateService updateService;

    private Result result = new Result();

    /**
     * 直接打分法
     */
    @RequestMapping(value = "/update1", method = RequestMethod.GET)
    @ApiOperation(value = "直接升级1", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rank_type", value = "规划参照的评估方案", paramType = "query", dataType = "String", defaultValue = "zhijie"),
            @ApiImplicitParam(name = "update_type", value = "规划选定的算法", paramType = "query", dataType = "String", defaultValue = "1"),
            @ApiImplicitParam(name = "nums", value = "规划给定的数值", paramType = "query", dataType = "String", defaultValue = "5")
    })
    public void update1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String rank_type = request.getParameter("rank_type");
        String update_type = request.getParameter("update_type");
        Integer nums = Integer.parseInt(request.getParameter("nums"));
        try{
            List<List<Map<String, Object>>> res = updateService.plan(rank_type,update_type,nums);
            result.packetResult(true, "直接打分法成功。", res, "0");
        }
        catch (Exception e){
            result.packetResult(false, "直接打分法不成功。获取参数失败或打分逻辑有误", null, "2001");
        }
        finally {
            JsonResult.toJson(result, response);
        }
    }


    /**
     * 给全市定个数打分法
     */
    @RequestMapping(value = "/update2", method = RequestMethod.GET)
    @ApiOperation(value = "全市升级", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rank_type", value = "规划参照的评估方案", paramType = "query", dataType = "String", defaultValue = "zhijie"),
            @ApiImplicitParam(name = "update_type", value = "规划选定的算法", paramType = "query", dataType = "String", defaultValue = "2"),
            @ApiImplicitParam(name = "nums", value = "规划给定的数值", paramType = "query", dataType = "String", defaultValue = "1000")
    })
    public void update2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String rank_type = request.getParameter("rank_type");
        String update_type = request.getParameter("update_type");
        Integer nums = Integer.parseInt(request.getParameter("nums"));
        try{
            List<List<Map<String, Object>>> res = updateService.plan(rank_type,update_type,nums);
            result.packetResult(true, "直接打分法成功。", res, "0");
        }
        catch (Exception e){
            result.packetResult(false, "直接打分法不成功。获取参数失败或打分逻辑有误", null, "2001");
        }
        finally {
            JsonResult.toJson(result, response);
        }
    }


    /**
     * 给全市定个数打分法
     */
    @RequestMapping(value = "/update3", method = RequestMethod.GET)
    @ApiOperation(value = "区域均衡升级", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rank_type", value = "规划参照的评估方案", paramType = "query", dataType = "String", defaultValue = "zhijie"),
            @ApiImplicitParam(name = "update_type", value = "规划选定的算法", paramType = "query", dataType = "String", defaultValue = "3"),
            @ApiImplicitParam(name = "nums", value = "规划给定的数值", paramType = "query", dataType = "String", defaultValue = "1000")
    })
    public void update3(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String rank_type = request.getParameter("rank_type");
        String update_type = request.getParameter("update_type");
        Integer nums = Integer.parseInt(request.getParameter("nums"));
        List<List<Map<String, Object>>> res = updateService.plan(rank_type,update_type,nums);
        try{

            result.packetResult(true, "直接打分法成功。", res, "0");
        }
        catch (Exception e){
            result.packetResult(false, "直接打分法不成功。获取参数失败或打分逻辑有误", null, "2001");
        }
        finally {
            JsonResult.toJson(result, response);
        }
    }

}
